﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using System.Data;
using DreamBuilder.Core.Container;
using DreamBuilder.DictionaryAccess;
using DreamBuilder.Common;
using DreamBuilder.Core.DAO;


namespace DreamBuilder.UI.UserControls.DataAccess.Components
{
    public class OracleUCViewDAL : UCViewDAL
    {
        public override string UCViewDBHelperString
        {
            get;set;
        }
        public override void GetChildTableData(ref System.Data.DataTable dtInfo, string childEditID, string parentKeyValue)
        {
            UCChildDAL uCChildDAL = UContainer.Resolve<UCChildDAL>();
            uCChildDAL.UCChildDBHelperString = UCViewDBHelperString;
            dtInfo = uCChildDAL.GetChildDataTable(childEditID, parentKeyValue);
            
        }

        public override System.Data.DataTable GetEditData(List<VoucherEditItemInfo> pEditInfoList, string ID, string parentKey, VOUCHERTABLETYPE tableViewType)
        {
            string sqlInfo = "";
            string selectSql = "";
            string fromSql = "";
            VoucherTotalInfo pTotalInfo = DictionaryAccessFacade.GetVoucherTotalInfo(ID);
            TableDicInfo pTableDicInfo = DictionaryAccessFacade.GetTableDicInfo(pTotalInfo.MainTable);
            if (string.IsNullOrEmpty(pTotalInfo.TableConnectSql))
            {
                fromSql = pTotalInfo.MainTable;
            }
            else
            {
                fromSql = pTotalInfo.TableConnectSql;
            }
            int columnIndex = 0;
            Dictionary<string, string> tempDictionary = new Dictionary<string, string>();
            pEditInfoList.ForSpecification<VoucherEditItemInfo>(
                delegate(VoucherEditItemInfo pEditItemInfo)
                {
                    string tableCode = pEditItemInfo.TableCode.Trim();
                    string fieldCode = pEditItemInfo.FieldCode.Trim();
                    FieldDicInfo pFieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(tableCode, fieldCode);

                    if (pEditItemInfo.ItemType == EDITITEMTYPE.TYPE_ComboBoxReference || pEditItemInfo.ItemType == EDITITEMTYPE.TYPE_Reference)
                    {
                        selectSql += tableCode + "." + fieldCode + " as "+ fieldCode + ",";
                        UCControlCommon.GetRefSelectAndFromSql(pFieldDicInfo, ref  fromSql, ref  selectSql, columnIndex);
                        tempDictionary.Add("COLUMN" + columnIndex.ToString(), tableCode + "__" + fieldCode);
                    }
                    else
                    {

                        selectSql += tableCode + "." + fieldCode + " as COLUMN" + columnIndex + ",";
                        tempDictionary.Add("COLUMN" + columnIndex.ToString(), tableCode + "__" + fieldCode);
                    }
                    columnIndex++;
                },
               delegate(VoucherEditItemInfo pEditItemInfo)
               {
                   return pEditItemInfo.Show == true ? true : false;
               });
            selectSql = selectSql.Substring(0, selectSql.Length - 1);
            sqlInfo = "select " + selectSql + " from " + fromSql + " where " + pTableDicInfo.TableCode + ".";
            parentKey = CStringFormat.SqlStrFormat2(parentKey).Trim();

            if (tableViewType == VOUCHERTABLETYPE.TYPE_BOTHER && !string.IsNullOrEmpty(pTableDicInfo.ParentConnectField))
            {
                sqlInfo += pTableDicInfo.ParentConnectField + " = '" + parentKey + "' ";
            }
            else
            {
                sqlInfo += pTableDicInfo.PrimaryField + " = '" + parentKey + "' ";
            }
            DataTable dtInfo =new DataTable();

            if (!string.IsNullOrEmpty(UCViewDBHelperString))
            {
                IDBHelper dbHelper = DBHelperFactory.CreateInstance(UCViewDBHelperString);
                dtInfo = dbHelper.ExecuteDataSet(sqlInfo).Tables[0];
            }
            foreach (string key in tempDictionary.Keys)
            {
                string aliasName = "";
                tempDictionary.TryGetValue(key, out aliasName);
                dtInfo.Columns[key].ColumnName = aliasName;
            }
            return dtInfo;
        }

    }
}
